<?php

$file = "/www/wwwroot/webhooksLog.txt";

$_File = fopen($file, "a+");

$local = '/www/wwwroot/mineEX';

//仓库地址
$remote = 'git@gitee.com:www9ckejinet/mineEX.git';

//密码
$password = '123456';

// 验证来源
if ($_SERVER['HTTP_USER_AGENT'] !== 'git-oschina-hook') {
    header('HTTP/1.1 403 Permission Denied');
    die('Permission denied.');
}

//获取请求参数
$request = file_get_contents('php://input');

if (empty($request)) {

  die('请求参数为空');

}
  

//验证密码是否正确
$data = json_decode($request, true);

if ($data['password'] != $password) {

  die('密码错误');

}

fwrite($_File, "************************************************\r\n");

fwrite($_File, "推送人Name: ");

fwrite($_File, $data["pusher"]["name"] . "\r\n");

fwrite($_File, "推送人Email: ");

fwrite($_File, $data["pusher"]["email"] . "\r\n");

fwrite($_File, "推送人UserName: ");

fwrite($_File, $data["pusher"]["username"] . "\r\n");

fwrite($_File, "推送人备注: ");

fwrite($_File, $data["head_commit"]["message"] . "\r\n");

fwrite($_File, "推送项目: ");

fwrite($_File, $data["repository"]["full_name"] . "\r\n");

fwrite($_File, "推送分支: ");

fwrite($_File, $data["ref"] . "\r\n");

fwrite($_File, "推送日期: ");

fwrite($_File, $data["head_commit"]["timestamp"] . "\r\n");

fwrite($_File, "************************************************\r\n");

fclose($_File);

$res = shell_exec("cd {$local} && git pull {$remote} 2>&1");

// system('cd '.$local.' && "C:/Program Files/Git/cmd/git.exe" pull '.$remote.' 2>&1', $out);

// echo $out;

die($res . date('Y-m-d H:i:s'));

?>